< previous page page_637 next page >

Page 637
Similarly, you can reduce the value of the depth parameter as rays pass through empty space. This is similar to the way the lighting model reduces diffuse and specular reflection as objects move farther from the light source.
Texture Mapping
The shading models used by ray-tracing programs create images that are quite realistic, but they are a bit too perfect to feel true to life. Light reflects off surfaces perfectly evenly. Shades of color blend with perfect smoothness as a surface curves away from the viewing position. Colors are often too pure to seem natural.
Texture mapping attempts to make ray-traced objects appear more realistic by modifying the ways their surfaces are drawn. They add images, texture, and uneven shading to the otherwise mathematically perfect ray-traced objects.
There are many different texture mapping algorithms, some of which are extremely complex. This chapter describes three of the simpler techniques: normal perturbation, environment mapping, and special environment mapping. They provide remarkable results with surprisingly simple code.
Normal Perturbation
After a ray-tracing program intersects a ray with a solid, it calculates the color of the point of intersection. The shading model uses a number of parameters to determine the point's color including the viewing position, the position of the light sources and other objects, and the surface's normal vector at that point. You can give an object a rough texture by simply changing its surface normal vectors by small random amounts.
The RayBumpySphere class demonstrates this technique. The following code shows how this object's FindHitColor function perturbs its normal vector. First it finds the sphere's surface normal as usual. It then adds a small random value to the vector's X, Y, and Z components. The value Bumpiness is a property of the object and should be fairly small. To randomize the normal vector, the program adds a random value between 0 and Bumpiness to the vector's X, Y, and Z components. The normal vector has length 1 when the random amounts are added, so setting Bumpiness to 0.2 or 0.4 produces a reasonably large effect.
After randomizing the vector's components, FindHitColor renormalizes the vector so that it again has length 1. It then calls the CalculateHitColor subroutine as usual passing it this modified vector.
e10a6a0a59f035e870d90cca05b85fce.gif
' Find the unit normal at this point.
Nx = px - Center.Trans(1)
Ny = py - Center.Trans(2)
Nz = pz - Center.Trans(3)
n_len = Sqr(Nx * Nx + Ny * Ny + Nz * Nz)
Nx = Nx / n_len
Ny = Ny / n_len
Nz = Nz / n_len

 
< previous page page_637 next page >